package com.qy.fortune.action;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.qy.common.AbstractController;
import com.qy.common.UserEcInfo;
import com.qy.fortune.ForTuneDataCache;
import com.qy.fortune.FortuneConstants;
import com.qy.fortune.service.FortuneService;

@Controller
@RequestMapping("/fortune")
public class FortuneController extends AbstractController{
	
	@Autowired
	FortuneService service;
	
	@Autowired
	private ForTuneDataCache fortuneDataCache;
	
	@RequestMapping("/calinfo") 
	public void calinfo(HttpServletRequest request,
			HttpServletResponse response,String year,String month,String day,String time,String sex,String name){
		UserEcInfo uei = new UserEcInfo(year,month,day,"12");
		String tg = "甲乙丙丁戊己庚辛壬癸";
		String rg = uei.getEight()[4];
		int i = tg.indexOf(rg);
		String c = FortuneConstants.characters[i]; //性格
		String ret = "命主 "+name+" 属 ["+uei.animalsYear()+"] 农历 "+uei.getLunarCalendar() +"</br>";
		ret += c;
		printJsonObjectData(ret, response);
	}
	
	@RequestMapping("/birthsec") 
	public void birthsec(HttpServletRequest request,
			HttpServletResponse response,String month,String day){
		printJsonObjectData(service.getBirthSec(month, day), response);
	}
	
	@RequestMapping("/fortunetoday") 
	public void fortunetoday(HttpServletRequest request,
			HttpServletResponse response,String year,String month,String day,String time,String sex){
		List<String> list = fortuneDataCache.getTodayFortune();

		Calendar c = Calendar.getInstance();
		int y = c.get(Calendar.YEAR);
		int m = c.get(Calendar.MONTH);
		int d = c.get(Calendar.DAY_OF_MONTH);
		
		int increase = y + m + d;
		int fixed = Integer.parseInt(year) + Integer.parseInt(month) + Integer.parseInt(day) + Integer.parseInt(time);
		int counter = increase + fixed;
		int len = list.size();
		String ret = list.get(counter%(len));
		printJsonObjectData(ret.split("[|]"), response);
	}
	
	@RequestMapping("/fortunetomorrow") 
	public void fortunetomorrow(HttpServletRequest request,
			HttpServletResponse response,String year,String month,String day,String time,String sex){
		List<String> list = fortuneDataCache.getTodayFortune();

		Calendar c = Calendar.getInstance();
		int y = c.get(Calendar.YEAR);
		int m = c.get(Calendar.MONTH);
		int d = c.get(Calendar.DAY_OF_MONTH);
		
		int increase = y + m + d;
		int fixed = Integer.parseInt(year) + Integer.parseInt(month) + Integer.parseInt(day) + Integer.parseInt(time);
		int counter = increase + fixed + 1;
		int len = list.size();
		String ret = list.get(counter%(len));
		printJsonObjectData(ret.split("[|]"), response);
	}
	
	@RequestMapping("/fortuneweek") 
	public void fortuneweek(HttpServletRequest request,
			HttpServletResponse response,String year,String month,String day,String time,String sex){
		String [][] dats = null;
		Calendar c = Calendar.getInstance();
		int y = c.get(Calendar.YEAR);
		int m = c.get(Calendar.MONTH);
		int d = c.get(Calendar.DAY_OF_MONTH);
		
		int increase = y + m + d;
		int fixed = Integer.parseInt(year) + Integer.parseInt(month) + Integer.parseInt(day) + Integer.parseInt(time);
		int counter = increase + fixed;
		if(sex.equals("男")){
			dats = FortuneConstants.fortune_boy;
		}else{
			dats = FortuneConstants.fortune_girl;
		}
		int len = dats.length;
		
		printJsonObjectData(dats[counter%len], response);
	}
	
	@RequestMapping("/fortuneweekscore") 
	public void fortuneweekscore(HttpServletRequest request,
			HttpServletResponse response,String year,String month,String day,String time,String sex){
		int [][] dats = {
				{83,54,81,57,56},
				{58,64,70,73,61},
				{84,70,60,72,82},
				{55,83,77,63,84},
				{61,50,70,54,83},
				{62,50,54,64,52},
				{73,73,88,82,72},
				{71,55,62,76,67},
				{55,65,69,59,72},
				{80,51,70,57,53},
				{55,78,90,80,61},
				{72,82,53,89,58},
				{90,74,62,81,54},
				{57,77,50,52,53},
				{77,62,59,75,87},
				{50,52,82,82,75},
				{56,84,77,66,65},
				{68,83,82,51,90},
				{86,63,59,60,82},
				{58,79,85,72,55},
				{71,83,59,64,87},
				{70,81,55,74,63},
				{80,78,89,68,74},
				{69,71,82,56,75},
				{51,55,50,67,73},
				{90,90,70,71,50},
				{83,82,90,76,70},
				{69,62,62,55,90},
				{73,70,50,80,80},
				{70,53,58,72,65},
				{52,77,65,70,57},
				{80,90,71,56,90},
				{75,80,80,88,86},
				{73,76,52,75,56},
				{62,53,53,77,62},
				{80,67,64,50,60},
				{80,67,66,58,84},
				{57,64,69,54,57},
				{54,81,88,87,65},
				{84,69,83,62,77},
				{83,53,89,68,51},
				{68,88,80,58,60},
				{65,64,62,60,69},
				{75,67,61,56,52},
				{50,55,79,85,82},
				{69,56,75,85,73},
				{72,61,70,60,66},
				{62,59,64,80,76},
				{90,84,56,61,78},
				{54,66,52,50,83},
				{54,53,69,77,57},
				{66,78,88,64,53},
				{70,50,59,85,60},
				{82,68,58,75,87},
				{67,50,56,73,71},
				{59,86,89,66,57},
				{77,71,50,73,74},
				{78,78,76,52,88}
		};
		Calendar c = Calendar.getInstance();
		int weekofyear = c.get(Calendar.WEEK_OF_YEAR);
		int fixed = Integer.parseInt(year) + Integer.parseInt(month) + Integer.parseInt(day) + Integer.parseInt(time);
		int counter = fixed + weekofyear;
		int i = counter%dats.length;
		printJsonObjectData(dats[i], response);
	}

	@RequestMapping("/fortunemonth") 
	public void fortunemonth(HttpServletRequest request,
			HttpServletResponse response,String year,String month,String day,String time,String sex){
		int [] datxq = {78,87,88,84,84,79,86,88,82,78,70,89,87,87,82,86,75,86,90,74,75,87,80,70,86,78,85,72,74,83,82,83,90,83,89,74,90,71,83,86,74,72,83,87,78,85,87,77,70,81,77,73,74,75,86,86,74,90,80,72,81,90,70,83,79,72,80,87,73,80,75,87,77,70,84,88,73,87,87,90,83,71,89,89,80,79,77,82,85,73,90,85,87,72,82,71,78,74,76,86,79,89,77,88,86,70,75,78,73,73,70,85,73,78,72,77,73,76,89,87,82,86,84,81,83,85,76,77,76,76,72,87,70,90,77,79,87,87,72,90,74,90,87,90,82,88,80,77,85,89,79,75,74,80,71,72,89,75,71,80,84,80,90,72,70,90,72,89,71,80,72,84,74,72,79,87,87,84,81,70,78,75,72,85,82,84,75,87,82,87,89,79,80,78,87,78,71,84,86,89,73,78,80,88,80,85,80,81,72,81,86,83,78,89,73,70,87,76,80,78,90,79,83,89,71,85,71,70,79,74,77,82,81,87,72,90,85,86,80,80,72,81,81,73,80,76,90,80,82,86,90,85,73,87,84,73,85,76,78,79,75,74,80,72,82,86,78,79,83,83,74,83,84,80,71,74,70,89,79,87,86,78,88,73,70,79,82,77,72,81,84,85,75,77,86,70,75,72,72,71,70,88,87,79,73,72,70,86,90,87,78,82,72,73,86,86,71,77,83,78,74,70,87,72,87,80,76,77,88,76,78,82,81,83,70,87,71,74,89,76,76,80,73,76,75,76,89,88,80,76,82,89,81,73,90,78,81,73,70,72,88,72,76,80,82};
		int [] dataq = {82,89,85,76,72,87,70,90,88,80,80,79,90,81,76,88,81,70,85,70,78,71,87,72,76,78,72,74,76,78,80,86,82,80,90,80,75,76,87,85,87,77,80,83,85,74,88,79,87,87,88,89,74,78,86,77,77,72,72,88,72,76,82,74,88,70,83,74,90,74,81,82,74,81,81,90,80,74,90,79,74,80,72,72,76,79,80,74,77,70,87,79,84,87,73,87,80,74,79,78,85,82,88,81,75,81,88,83,80,82,72,80,73,81,86,87,83,77,81,87,86,77,89,78,78,79,83,80,76,87,75,79,89,85,87,73,76,74,80,90,86,85,82,73,90,90,74,71,80,89,86,85,76,76,82,71,83,85,80,81,73,85,71,86,76,84,81,87,90,77,88,89,77,75,71,85,75,87,82,87,88,70,89,90,83,84,87,78,78,86,83,76,78,85,71,78,72,70,79,80,73,88,70,79,70,83,75,90,87,74,90,90,74,88,70,78,73,74,82,76,82,83,82,86,80,79,80,80,84,89,89,80,82,74,75,88,89,73,86,70,74,86,83,72,90,70,75,81,84,70,71,78,87,81,90,85,74,82,84,89,77,75,89,70,87,76,79,79,70,82,74,77,81,71,81,77,85,86,80,76,89,77,76,71,81,89,74,70,80,84,71,73,75,86,85,89,87,81,77,81,76,88,81,89,71,83,76,86,77,71,88,72,79,78,73,88,79,73,77,74,81,82,74,89,79,88,88,80,87,87,84,81,86,89,70,70,89,78,80,90,76,83,81,82,90,72,84,79,80,76,79,88,79,90,89,75,86,88,84,73,81,87,87,76,70};
		int [] datcy = {86,72,89,77,86,80,86,73,74,77,79,86,78,76,85,83,70,73,79,70,89,70,88,79,75,83,79,90,89,71,79,77,77,80,84,90,76,79,87,81,70,85,79,78,72,83,75,70,70,88,83,88,90,86,71,88,85,86,72,77,90,90,89,83,73,78,73,72,89,72,88,85,90,80,73,72,87,82,80,79,81,89,82,78,87,89,87,86,70,78,73,89,90,87,71,79,80,84,81,72,81,83,88,79,80,77,70,71,88,89,77,84,86,72,78,83,85,88,72,79,86,72,89,77,88,83,81,85,72,73,77,73,81,84,80,80,70,82,70,85,88,89,84,82,80,87,73,74,70,90,87,74,80,78,83,82,86,76,83,85,70,82,90,82,75,90,74,83,77,76,79,80,75,79,70,80,81,75,78,74,87,82,77,89,73,80,78,88,76,86,86,89,90,76,70,88,76,83,78,76,72,86,90,83,83,86,78,75,79,74,88,71,82,79,82,76,76,90,70,70,73,87,76,71,86,88,79,74,78,78,70,88,84,71,84,84,89,82,78,71,88,70,88,74,83,75,87,79,81,70,90,70,78,85,78,85,74,73,71,74,71,72,86,83,83,84,72,73,70,90,78,86,71,71,84,82,84,77,80,74,77,73,73,90,89,90,87,82,90,85,77,83,80,73,84,72,85,76,78,76,72,71,72,72,73,80,82,76,72,84,76,89,88,81,85,79,75,72,85,75,84,88,83,84,81,79,72,90,80,79,84,89,82,84,80,87,70,86,80,82,72,85,72,79,79,81,82,87,87,86,80,74,74,84,87,80,83,88,74,78,76,80,88,78,87};
		int [] datgz = {87,89,85,81,79,89,72,81,82,81,70,88,90,81,71,85,77,73,83,78,81,75,85,80,73,71,89,80,90,83,70,81,70,86,78,85,74,71,76,72,89,77,72,78,82,70,83,78,74,81,81,72,71,78,88,80,83,75,77,88,70,86,84,72,77,70,82,81,82,87,72,82,77,78,87,85,79,87,74,79,78,77,89,89,78,78,79,70,85,73,89,88,72,80,70,71,78,82,87,84,72,70,86,77,85,70,84,75,71,85,79,73,74,86,71,74,81,89,87,74,70,75,85,74,71,76,87,79,76,71,82,90,84,84,90,77,88,72,77,85,78,79,88,74,75,76,71,89,76,87,78,74,71,86,76,71,86,86,89,73,88,71,81,86,75,85,71,74,77,76,72,80,70,70,79,84,83,89,79,81,89,89,79,78,90,81,78,89,83,77,77,70,74,71,83,89,84,89,73,88,80,78,74,73,79,79,80,83,74,90,89,74,70,86,85,90,71,70,81,76,73,87,84,78,90,72,89,78,75,88,80,78,77,87,79,77,84,81,78,84,71,83,72,80,72,77,90,86,88,85,75,75,77,72,77,76,86,70,82,82,75,74,81,70,87,75,86,80,76,72,83,78,71,75,82,90,89,80,72,78,87,88,87,85,74,86,82,90,88,73,70,78,79,75,80,74,87,77,85,79,88,77,82,85,89,81,83,90,81,73,90,82,85,77,75,82,78,77,80,71,78,88,88,85,75,90,89,89,88,70,86,82,76,85,76,77,72,81,86,77,83,90,80,81,81,71,90,78,88,72,72,80,90,83,76,75,74,83,84,90,77,89,82,76,85};
		Calendar c = Calendar.getInstance();
		int y = c.get(Calendar.YEAR);
		int m = c.get(Calendar.MONTH);
		int d = c.get(Calendar.DAY_OF_MONTH);
		
		int increase = y + m + d;
		int fixed = Integer.parseInt(year) + Integer.parseInt(month) + Integer.parseInt(day) + Integer.parseInt(time);
		int counter = increase + fixed;
		int seek = counter % 365;
		
		List<String> xqlist = fortuneDataCache.getXqFortune();
		List<String> aqlist = fortuneDataCache.getAqFortune();
		List<String> gzlist = fortuneDataCache.getGzFortune();
		List<String> cylist = fortuneDataCache.getCyFortune();
		
		int ret[][] = {get30(datxq,seek),get30(dataq,seek),get30(datcy,seek),get30(datgz,seek)};
		Map ret_map = new HashMap();
		ret_map.put("score", ret);
		ret_map.put("心情", get30(xqlist,seek));
		ret_map.put("爱情", get30(aqlist,seek));
		ret_map.put("财运", get30(cylist,seek));
		ret_map.put("工作", get30(gzlist,seek));
		printJsonObjectData(ret_map, response);
	}
	
	private String[] get30(List<String> list,int num){
		String ret[] = new String[15];
		int j=0;
		for(int i=num;i<(num+15);i++){
			if(i >= list.size()){
				ret[j++] = list.get(i - list.size());
			}else{
				ret[j++] = list.get(i);
			}
		}
		return ret;
	} 
	
	private int[] get30(int[] dat,int start){
		int[] ret = new int[15];
		int j=0;
		for(int i=start;i<(15+start);i++){
			if(i >= dat.length){
				ret[j++] = dat[i-dat.length];
			}else{
				ret[j++] = dat[i];
			}
		}
		return ret;
	}
}
